//
//  LoginView.m
//  02-LaGouRegisterHomeWork
//
//  Created by 曹曹 on 16/7/15.
//  Copyright © 2016年 曹曹. All rights reserved.
//

#import "LoginView.h"

#define WIDTH frame.size.width
#define HEIGHT (frame.size.height - 1) / 2

@implementation LoginView

- (instancetype)initWithFrame:(CGRect)frame imageUser:(NSString *)imageUser
                imagePassword:(NSString *)imagePassword
              placeHolderUser:(NSString *)placeHolderUser
      placeHolderDownPassword:(NSString *)placeHolderPassword {
    self = [super initWithFrame:frame];
    if (self) {
        [self configLoginTextFieldWithFrame:frame imageUser:imageUser
                              imagePassword:imagePassword
                            placeHolderUser:placeHolderUser
                    placeHolderDownPassword:placeHolderPassword];
        [self createTextFieldLeftViewWithUserImage:imageUser passwordImage:imagePassword];
        [self drawRoundedView];
    }
    return self;
}
- (void)configLoginTextFieldWithFrame:(CGRect)frame imageUser:(NSString *)imageUser
                        imagePassword:(NSString *)imagePassword
                      placeHolderUser:(NSString *)placeHolderUser
              placeHolderDownPassword:(NSString *)placeHolderPassword {
    CGRect frameUser = CGRectMake(0, 0, WIDTH, HEIGHT);
    CGRect framePassword = CGRectMake(0, 45, WIDTH, HEIGHT);
    _textFieldUser = [[UITextField alloc] initWithFrame:frameUser];
    _textFieldPassword = [[UITextField alloc] initWithFrame:framePassword];
#pragma mark - 签代理
    _textFieldUser.delegate = self;
    _textFieldPassword.delegate = self;
    if ([imageUser isEqualToString:@"icon_username"]) {
        _textFieldUser.tag = 333;
        _textFieldPassword.tag = 666;
    }
    if ([imageUser isEqualToString:@"phone"]) {
        _textFieldUser.tag = 555;
        _textFieldPassword.tag = 888;
        UIButton *vertificationButton = [UIButton buttonWithType:UIButtonTypeSystem];
        vertificationButton.frame = CGRectMake(200, 0, 100, 44);
        [vertificationButton setTitle:@"获取验证码" forState:UIControlStateNormal];
        [vertificationButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
        self.textFieldPassword.rightView = vertificationButton;
        _textFieldPassword.rightViewMode = UITextFieldViewModeAlways;
    }
    _textFieldUser.backgroundColor = [UIColor blackColor];
    _textFieldPassword.backgroundColor = [UIColor blackColor];
    _textFieldUser.textColor = [UIColor whiteColor];
    _textFieldPassword.textColor = [UIColor whiteColor];
    _textFieldUser.alpha = 0.4;
    _textFieldPassword.alpha = 0.4;
    if ([imageUser isEqualToString:@"icon_username"]) {
        _textFieldPassword.secureTextEntry = YES;
    }
    [self addSubview:_textFieldUser];
    [self addSubview:_textFieldPassword];
    _textFieldUser.placeholder = placeHolderUser;
    _textFieldPassword.placeholder = placeHolderPassword;
#pragma mark - 设置textField的placeHolder的字体颜色
    [_textFieldUser setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
    [_textFieldPassword setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
    _textFieldUser.leftViewMode = UITextFieldViewModeAlways;
    _textFieldPassword.leftViewMode = UITextFieldViewModeAlways;
#pragma mark - 加黑处理图标
    UIView *viewUser = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
    UIView *viewPass = [[UIView alloc] initWithFrame:CGRectMake(0, 45, 44, 44)];
    [self insertSubview:viewUser atIndex:0];
    [self insertSubview:viewPass atIndex:0];
    viewPass.backgroundColor = [UIColor blackColor];
    viewUser.backgroundColor = [UIColor blackColor];
    viewPass.alpha = 0.7;
    viewUser.alpha = 0.7;
    /** 上边的view */
    UIBezierPath *maskPathViewTop = [UIBezierPath bezierPathWithRoundedRect:viewUser.bounds byRoundingCorners:UIRectCornerTopLeft cornerRadii:CGSizeMake(5, 5)];
    CAShapeLayer *maskLayerViewTop = [[CAShapeLayer alloc] init];
    maskLayerViewTop.frame = viewUser.bounds;
    maskLayerViewTop.path = maskPathViewTop.CGPath;
    viewUser.layer.mask = maskLayerViewTop;
    /** 下边的view */
    UIBezierPath *maskPathViewDown = [UIBezierPath bezierPathWithRoundedRect:viewPass.bounds byRoundingCorners:UIRectCornerBottomLeft cornerRadii:CGSizeMake(5, 5)];
    CAShapeLayer *maskLayerViewDown = [[CAShapeLayer alloc] init];
    maskLayerViewDown.frame = viewPass.bounds;
    maskLayerViewDown.path = maskPathViewDown.CGPath;
    viewPass.layer.mask = maskLayerViewDown;
}
#pragma mark - 左视图
- (void)createTextFieldLeftViewWithUserImage:(NSString *)user passwordImage:(NSString *)password {
    /** 用户 */
    _buttonOfUser = [UIButton buttonWithType:UIButtonTypeCustom];
    [_buttonOfUser setImage:[[UIImage imageNamed:user] imageWithRenderingMode:1] forState:UIControlStateNormal];
    [self addSubview:_buttonOfUser];
    _buttonOfUser.frame = CGRectMake(0, 0, 44, 44);
    _buttonOfUser.backgroundColor = [UIColor blackColor];
    _buttonOfUser.alpha = 0.8;
    _textFieldUser.leftViewMode = UITextFieldViewModeAlways;
    _textFieldUser.leftView = _buttonOfUser;
    _textFieldUser.leftView.backgroundColor = [UIColor blackColor];
    /** 密码 */
    _buttonOfPassword = [UIButton buttonWithType:UIButtonTypeCustom];
    [_buttonOfPassword setImage:[[UIImage imageNamed:password] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [self addSubview:_buttonOfPassword];
    _buttonOfPassword.frame = CGRectMake(0, 45, 44, 44);
    _buttonOfPassword.backgroundColor = [UIColor blackColor];
    _buttonOfPassword.alpha = 0.8;
    _textFieldPassword.leftViewMode = UITextFieldViewModeAlways;
    _textFieldPassword.leftView = _buttonOfPassword;
}
#pragma mark - 画圆角
- (void)drawRoundedView {
    /** 用户 */
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:_textFieldUser.bounds byRoundingCorners:UIRectCornerTopRight | UIRectCornerTopLeft cornerRadii:CGSizeMake(5, 5)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = _textFieldUser.bounds;
    maskLayer.path = maskPath.CGPath;
    _textFieldUser.layer.mask = maskLayer;
    /** 密码 */
    UIBezierPath *maskPathP = [UIBezierPath bezierPathWithRoundedRect:_textFieldPassword.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(5, 5)];
    CAShapeLayer *maskLayerP = [[CAShapeLayer alloc] init];
    maskLayerP.frame = _textFieldPassword.bounds;
    maskLayerP.path = maskPathP.CGPath;
    _textFieldPassword.layer.mask = maskLayerP;
}
#pragma mark - 回车键回收键盘
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}
#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    if (textField.tag == 333) {
        _buttonOfUser.selected = !_buttonOfUser.selected;
        [_buttonOfUser setImage:[UIImage imageNamed:@"icon_username_select"] forState:UIControlStateSelected];
    }
    if (textField.tag == 666) {
        _buttonOfPassword.selected = !_buttonOfPassword.selected;
        [_buttonOfPassword setImage:[UIImage imageNamed:@"icon_password_select"] forState:UIControlStateSelected];
    }
    if (textField.tag == 555) {
        _buttonOfUser.selected = !_buttonOfUser.selected;
        [_buttonOfUser setImage:[UIImage imageNamed:@"phone_selected"] forState:UIControlStateSelected];
    }
    if (textField.tag == 888) {
        self.buttonOfPassword.selected = !_buttonOfPassword.selected;
        [self.buttonOfPassword setImage:[UIImage imageNamed:@"vertification_selected"] forState:UIControlStateSelected];
    }
    return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    if (textField.tag == 333) {
        _buttonOfUser.selected = !_buttonOfUser.selected;
        [_buttonOfUser setImage:[UIImage imageNamed:@"icon_username"] forState:UIControlStateSelected];
    }
    if (textField.tag == 666) {
        _buttonOfPassword.selected = !self.buttonOfPassword.selected;
        [self.buttonOfPassword setImage:[UIImage imageNamed:@"icon_password"] forState:UIControlStateSelected];
    }
    if (textField.tag == 555) {
        _buttonOfUser.selected = !_buttonOfUser.selected;
        [_buttonOfUser setImage:[UIImage imageNamed:@"phone"] forState:UIControlStateSelected];
    }
    if (textField.tag == 888) {
        _buttonOfPassword.selected = !self.buttonOfPassword.selected;
        [_buttonOfPassword setImage:[UIImage imageNamed:@"vertification"] forState:UIControlStateSelected];
    }
    return YES;
}
@end
